************************************************************************************************************
* This file produces Figures III, IV, and V in the paper
************************************************************************************************************

* Read in House Price Data
import excel "House Riskiness Analysis (Table II).xlsx", sheet("house_prices_stata") firstrow clear
rename A year

foreach country in "Australia" "Belgium" "Canada" "Denmark" "Finland" "France" "Germany" "Japan" "Italy" "Netherlands" "NewZealand" "Norway" "Singapore" "Spain" "SouthAfrica" "SouthKorea" "Sweden" "Switzerland" "UK" "USA" {
	rename `country' `country'_HPI
}

sort year
tempfile temp_hpi
save `temp_hpi'


*****************************
* Read in consumption data
*****************************

import excel "barro_ursua_macrodataset_1110.xls", sheet("C") firstrow clear
rename Cpc year
destring(year), replace force
drop if year == .
destring(*), replace
rename UnitedKingdom UK
rename UnitedStates USA
rename SAfrica SouthAfrica
rename Korea SouthKorea

foreach country in "Australia" "Belgium" "Canada" "Denmark" "Finland" "France" "Germany" "Japan" "Italy" "Netherlands" "NewZealand" "Norway" "Singapore" "Spain" "SouthAfrica" "SouthKorea" "Sweden" "Switzerland" "UK" "USA" {
	rename `country' `country'_C
}

keep year *_C

sort year
tempfile temp_c
save `temp_c'

*****************************
* Read in GDP data
*****************************

import excel "barro_ursua_macrodataset_1110.xls", sheet("GDP") firstrow clear
rename GDPpc year
destring(year), replace force
drop if year == .
destring(*), replace
rename UnitedKingdom UK
rename UnitedStates USA
rename SAfrica SouthAfrica
rename Korea SouthKorea

foreach country in "Australia" "Belgium" "Canada" "Denmark" "Finland" "France" "Germany" "Japan" "Italy" "Netherlands" "NewZealand" "Norway" "Singapore" "Spain" "SouthAfrica" "SouthKorea" "Sweden" "Switzerland" "UK" "USA" {
	rename `country' `country'_GDP
}

keep year *_GDP

sort year
tempfile temp_gdp
save `temp_gdp'

*************************************************
* CREATE DATASET
*************************************************

mmerge year using `temp_hpi'
mmerge year using `temp_c'

sort year 

foreach country in "Australia" "Belgium" "Canada" "Denmark" "Finland" "France" "Germany" "Japan" "Italy" "Netherlands" "NewZealand" "Norway" "Singapore" "Spain" "SouthAfrica" "SouthKorea" "Sweden" "Switzerland" "UK" "USA" {
	cap gen `country'_HPI_delta = (`country'_HPI - `country'_HPI[_n-1]) / `country'_HPI[_n-1]
	cap gen `country'_C_delta   = (`country'_C   - `country'_C[_n-1])   / `country'_C[_n-1]
	cap gen `country'_GDP_delta = (`country'_GDP - `country'_GDP[_n-1]) / `country'_GDP[_n-1]
}


**********************************************************************************************
**********************************************************************************************
*
* TAYLOR AND SCHULARICK DATES (updated with R&R and Bordo)
*
**********************************************************************************************
**********************************************************************************************

***********************************************
* NORWAY
***********************************************

tsset year
tsline Norway_HPI if Norway_HPI != ., title(Norway and Schularick + Taylor Dates) scheme(s1color) xtitle("") name(fig_norway, replace) ///
                                      tline(1899) tline(1922) tline(1931) tline(1988) nodraw

gen     date_norway = 0
replace date_norway = 1 if inlist(year,1899, 1922, 1931, 1988)

cap drop relative_to_crisis_norway
gen relative_to_crisis_norway = .

cap drop index_norway
gen      index_norway = 0
gen      index_norway_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_norway = `i' if date_norway[_n+`j'] == 1
	replace index_norway              = Norway_HPI / Norway_HPI[_n+`j'] if relative_to_crisis_norway == `i'
	replace index_norway_c            = Norway_C / Norway_C[_n+`j'] if relative_to_crisis_norway == `i'
}


***********************************************
* SWEDEN
***********************************************

tsline Sweden_HPI if Sweden_HPI != ., title(Sweden and Schularick + Taylor Dates) scheme(s1color) xtitle("") name(fig_sweden, replace) ///
                                      tline(1878) tline(1907) tline(1922) tline(1931) tline(1991) tline(2008) nodraw

gen     date_sweden = 0
replace date_sweden = 1 if inlist(year,1878, 1907, 1922, 1931, 1991, 2008)

gen relative_to_crisis_sweden = .

cap drop index_sweden
gen      index_sweden = 0
gen      index_sweden_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_sweden = `i' if date_sweden[_n+`j'] == 1
	replace index_sweden              = Sweden_HPI / Sweden_HPI[_n+`j'] if relative_to_crisis_sweden == `i'
	replace index_sweden_c            = Sweden_C / Sweden_C[_n+`j'] if relative_to_crisis_sweden == `i'
}

***********************************************
* NETHERLANDS
***********************************************

tsline Netherlands_HPI if Netherlands_HPI != . , title(Netherlands and Schularick + Taylor Dates) scheme(s1color) xtitle("") name(fig_netherlands, replace) yscale(log) ///
                                                 tline(1893) tline(1907) tline(1921) tline(1939) tline(2008) nodraw
												 
gen     date_netherlands = 0
replace date_netherlands = 1 if inlist(year,1893, 1907, 1921, 1939, 2008)

cap drop relative_to_crisis_netherlands
gen relative_to_crisis_netherlands = .

cap drop index_netherlands
gen      index_netherlands = 0
gen      index_netherlands_c = 0


forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_netherlands = `i' if date_netherlands[_n+`j'] == 1
	replace index_netherlands              = Netherlands_HPI / Netherlands_HPI[_n+`j'] if relative_to_crisis_netherlands == `i'
	replace index_netherlands_c            = Netherlands_C / Netherlands_C[_n+`j'] if relative_to_crisis_netherlands == `i'
}

***********************************************
* ITALY
***********************************************

tsline Italy_HPI if Italy_HPI != . , title(Italy and Schularick + Taylor Dates) scheme(s1color) xtitle("") name(fig_italy, replace) yscale(log) ///
								     tline(1873) tline(1887) tline(1891) tline(1907) tline(1921) tline(1930) tline(1935) tline(1990) tline(2008) nodraw

gen     date_italy = 0
replace date_italy = 1 if inlist(year,1873, 1891, 1907, 1921, 1930, 1935, 1990, 2008)

cap drop relative_to_crisis_italy
gen relative_to_crisis_italy = .

cap drop index_italy
gen      index_italy = 0
gen      index_italy_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_italy = `i' if date_italy[_n+`j'] == 1
	replace index_italy              = Italy_HPI / Italy_HPI[_n+`j'] if relative_to_crisis_italy == `i'
	replace index_italy_c            = Italy_C / Italy_C[_n+`j'] if relative_to_crisis_italy == `i'
}

***********************************************
* FRANCE
**********************************************

tsline France_HPI if France_HPI != ., title(France and Schularick + Taylor Dates) scheme(s1color) yscale(log) xtitle("") name(fig_france, replace)   ///
                                      tline(1882) tline(1889) tline(1907) tline(1930) tline(2008) nodraw

gen     date_france = 0
replace date_france = 1 if inlist(year,1882, 1889, 1907, 1930, 2008)

gen relative_to_crisis_france = .

cap drop index_france
gen      index_france = 0
gen      index_france_c = 0


forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_france = `i' if date_france[_n+`j'] == 1
	replace index_france              = France_HPI / France_HPI[_n+`j'] if relative_to_crisis_france == `i'
	replace index_france_c            = France_C / France_C[_n+`j'] if relative_to_crisis_france == `i'
}


***********************************************
* DENMARK
**********************************************

tsline Denmark_HPI if Denmark_HPI != ., title(Denmark and Schularick + Taylor Dates) scheme(s1color) xtitle("") name(fig_denmark, replace)   ///
                                        tline(1877) tline(1885) tline(1902)  tline(1907) tline(1921) tline(1931) tline(1987) yscale(log) nodraw

gen     date_denmark = 0
replace date_denmark = 1 if inlist(year,1882, 1889, 1907, 1930)

cap drop relative_to_crisis_denmark
gen relative_to_crisis_denmark = .

cap drop index_denmark
gen      index_denmark = 0
gen      index_denmark_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_denmark = `i' if date_denmark[_n+`j'] == 1
	replace index_denmark              = Denmark_HPI / Denmark_HPI[_n+`j'] if relative_to_crisis_denmark == `i'
	replace index_denmark_c            = Denmark_C / Denmark_C[_n+`j'] if relative_to_crisis_denmark == `i'
}


***********************************************
* USA
**********************************************

tsline USA_HPI if USA_HPI != ., title(USA and Schularick + Taylor Dates) scheme(s1color) name(fig_usa, replace) yscale(log) ///
							    tline(1873) tline(1884) tline(1893)  tline(1907) tline(1929) tline(1984) tline(2007) nodraw

gen     date_usa = 0
replace date_usa = 1 if inlist(year,1873, 1884, 1893, 1907, 1929, 1984, 2007)

gen relative_to_crisis_usa = .

cap drop index_usa
gen      index_usa = 0
gen      index_usa_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_usa = `i' if date_usa[_n+`j'] == 1
	replace index_usa              = USA_HPI / USA_HPI[_n+`j'] if relative_to_crisis_usa == `i'
	replace index_usa_c            = USA_C / USA_C[_n+`j'] if relative_to_crisis_usa == `i'
}


************************************************************************************
* Japan
************************************************************************************

tsline Japan_HPI if Japan_HPI != ., title(Japan and Schularick + Taylor Dates) scheme(s1color) name(fig_japan, replace) ///
								    tline(1882) tline(1900) tline(1904) tline(1907) tline(1913) tline(1927) tline(1992) yscale(log) nodraw 

gen     date_japan = 0
replace date_japan = 1 if inlist(year,1882, 1900, 1904, 1907, 1913, 1927, 1992)

gen relative_to_crisis_japan  = .

cap drop index_japan
gen      index_japan = 0
gen      index_japan_c = 0


forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_japan = `i' if date_japan[_n+`j'] == 1
	replace index_japan              = Japan_HPI / Japan_HPI[_n+`j'] if relative_to_crisis_japan == `i'
	replace index_japan_c            = Japan_C / Japan_C[_n+`j'] if relative_to_crisis_japan == `i'
}

************************************************************************************
* Canada
************************************************************************************

tsline Canada_HPI if Canada_HPI != ., title(Canada and Schularick + Taylor Dates) scheme(s1color) yscale(log) name(fig_canada, replace) nodraw

gen     date_canada = 0
replace date_canada = 1 if inlist(year,1873, 1907, 1923)

gen relative_to_crisis_canada  = .

cap drop index_canada
gen      index_canada = 0
gen      index_canada_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_canada = `i' if date_canada[_n+`j'] == 1
	replace index_canada              = Canada_HPI / Canada_HPI[_n+`j'] if relative_to_crisis_canada == `i'
	replace index_canada_c            = Canada_C / Canada_C[_n+`j'] if relative_to_crisis_canada == `i'
}


************************************************************************************
* Australia
************************************************************************************

tsline Australia_HPI if Australia_HPI != ., title(Australia and Schularick + Taylor Dates) scheme(s1color) name(fig_australia, replace) yscale(log) ///
                                            tline(1893) tline(1989) nodraw

gen     date_australia = 0
replace date_australia = 1 if inlist(year,1893, 1989)

gen relative_to_crisis_australia  = .

cap drop index_australia
gen      index_australia = 0
gen      index_australia_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_australia = `i' if date_australia[_n+`j'] == 1
	replace index_australia              = Australia_HPI / Australia_HPI[_n+`j'] if relative_to_crisis_australia == `i'
	replace index_australia_c            = Australia_C / Australia_C[_n+`j'] if relative_to_crisis_australia == `i'
}

************************************************************************************
* Germany
************************************************************************************

tsline Germany_HPI if Germany_HPI != ., title(Germany and Schularick + Taylor Dates) scheme(s1color) name(fig_germany, replace) yscale(log) ///
                                            tline(1873) tline(1891) tline(1901) tline(1907) tline(1931) tline(2008) nodraw

gen     date_germany = 0
replace date_germany = 1 if inlist(year,1873, 1891, 1901, 1907, 1931, 2008)

gen relative_to_crisis_germany  = .

cap drop index_germany
gen      index_germany = 0
gen      index_germany_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_germany = `i' if date_germany[_n+`j'] == 1
	replace index_germany              = Germany_HPI / Germany_HPI[_n+`j'] if relative_to_crisis_germany == `i'
	replace index_germany_c            = Germany_C / Germany_C[_n+`j'] if relative_to_crisis_germany == `i'
}


************************************************************************************
* Spain
************************************************************************************

tsline Spain_HPI if Spain_HPI != ., title(Spain and Schularick + Taylor Dates) scheme(s1color) name(fig_spain, replace) yscale(log) ///
                                            tline(1883) tline(1890) tline(1913) tline(1920) tline(1924) tline(1931) tline(1978) tline(2008) nodraw

gen     date_spain = 0
replace date_spain = 1 if inlist(year, 1883, 1890, 1913, 1920, 1924, 1931, 1978, 2008)

gen relative_to_crisis_spain  = .

cap drop index_spain
gen      index_spain = 0
gen      index_spain_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_spain = `i' if date_spain[_n+`j'] == 1
	replace index_spain              = Germany_HPI / Germany_HPI[_n+`j'] if relative_to_crisis_spain == `i'
	replace index_spain_c            = Germany_C / Germany_C[_n+`j'] if relative_to_crisis_spain == `i'
}

************************************************************************************
* Singapore
************************************************************************************

gen crisis = 0
replace crisis = 1 if inlist(year,1982,1983, 1997,1998,2007,2008)
twoway (bar crisis year if year > 1975 & year < 2012, lpattern(none) lcolor(gray) yscale(log) yaxis(2) yscale(alt axis(2)) color(gray) lwidth(none)) ///
       (tsline Singapore_HPI if year > 1975 & year < 2012, yscale(alt) lcolor(blue) lwidth(thick)), ///
	   name(fig_spain, replace) ytitle("Real House Prices (Log Scale)")  yscale(axis(1) log)  ytitle("", axis(2)) yscale(off axis(2)) xtitle("") legend(off) scheme(s1mono)

cap graph export "SG_Timeseries_Graph.pdf", replace 			
cap drop crisis

gen     date_singapore = 0
replace date_singapore = 1 if inlist(year, 1982)

gen relative_to_crisis_singapore  = .

cap drop index_singapore
gen      index_singapore = 0
gen      index_singapore_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_singapore = `i' if date_singapore[_n+`j'] == 1
	replace index_singapore              = Singapore_HPI / Singapore_HPI[_n+`j'] if relative_to_crisis_singapore == `i'
	replace index_singapore_c            = Singapore_C / Singapore_C[_n+`j'] if relative_to_crisis_singapore == `i'
}


************************************************************************************
* UK
************************************************************************************

gen crisis = 0
replace crisis = 1 if inlist(year,1974,1975,1976,1984,1991,2007,2008,2009)
twoway (bar crisis year if year > 1955, lpattern(none) lcolor(gray) yscale(log) yaxis(2) yscale(alt axis(2)) color(gray) lwidth(none)) ///
       (tsline UK_HPI if year > 1955, yscale(alt) lcolor(blue) lwidth(thick)), ///
	   name(fig_spain, replace) ytitle("Real House Prices (Log Scale)")  yscale(axis(1) log)  ytitle("", axis(2)) yscale(off axis(2)) xtitle("") legend(off) scheme(s1mono)

cap graph export "UK_Timeseries_Graph.pdf", replace 			
cap drop crisis

gen     date_uk = 0
replace date_uk = 1 if inlist(year, 1873,1890,1974,1984,1991,2007)

gen relative_to_crisis_uk  = .

cap drop index_uk
gen      index_uk = 0
gen      index_uk_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_uk = `i' if date_uk[_n+`j'] == 1
	replace index_uk              = UK_HPI / UK_HPI[_n+`j'] if relative_to_crisis_uk == `i'
	replace index_uk_c            = UK_C / UK_C[_n+`j'] if relative_to_crisis_uk == `i'
}



************************************************************************************
* Switzerland
************************************************************************************

gen     date_switzerland = 0
replace date_switzerland = 1 if inlist(year, 1870,1910,1931,2008)

gen relative_to_crisis_switzerland  = .

cap drop index_switzerland
gen      index_switzerland = 0
gen      index_switzerland_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_switzerland = `i' if date_switzerland[_n+`j'] == 1
	replace index_switzerland              = Switzerland_HPI / Switzerland_HPI[_n+`j'] if relative_to_crisis_switzerland == `i'
	replace index_switzerland_c            = Switzerland_C   / Switzerland_C[_n+`j']   if relative_to_crisis_switzerland == `i'
}


************************************************************************************
* Belgium
************************************************************************************

gen     date_belgium = 0
replace date_belgium = 1 if inlist(year, 2008)

gen relative_to_crisis_belgium  = .

cap drop index_belgium
gen      index_belgium = 0
gen      index_belgium_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_belgium = `i' if date_belgium[_n+`j'] == 1
	replace index_belgium              = Belgium_HPI / Belgium_HPI[_n+`j'] if relative_to_crisis_belgium == `i'
	replace index_belgium_c            = Belgium_C / Belgium_C[_n+`j'] if relative_to_crisis_belgium == `i'
}


************************************************************************************
* Finland
************************************************************************************

gen     date_finland = 0
replace date_finland = 1 if inlist(year, 1991)

gen relative_to_crisis_finland  = .

cap drop index_finland
gen      index_finland = 0
gen      index_finland_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_finland = `i' if date_finland[_n+`j'] == 1
	replace index_finland              = Finland_HPI / Finland_HPI[_n+`j'] if relative_to_crisis_finland == `i'
	replace index_finland_c            = Finland_C / Finland_C[_n+`j'] if relative_to_crisis_finland == `i'
}


************************************************************************************
* New Zealand
************************************************************************************

gen     date_newzealand = 0
replace date_newzealand = 1 if inlist(year, 1987)

gen relative_to_crisis_newzealand  = .

cap drop index_newzealand
gen      index_newzealand = 0
gen      index_newzealand_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_newzealand = `i' if date_newzealand[_n+`j'] == 1
	replace index_newzealand              = NewZealand_HPI / NewZealand_HPI[_n+`j'] if relative_to_crisis_newzealand == `i'
	replace index_newzealand_c            = NewZealand_C / NewZealand_C[_n+`j'] if relative_to_crisis_newzealand == `i'
}



************************************************************************************
* South Korea
************************************************************************************

gen     date_southkorea = 0
replace date_southkorea = 1 if inlist(year, 1986, 1997)

gen relative_to_crisis_southkorea  = .

cap drop index_southkorea
gen      index_southkorea = 0
gen      index_southkorea_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_southkorea = `i' if date_southkorea[_n+`j'] == 1
	replace index_southkorea              = SouthKorea_HPI / SouthKorea_HPI[_n+`j'] if relative_to_crisis_southkorea == `i'
	replace index_southkorea_c            = SouthKorea_C / SouthKorea_C[_n+`j'] if relative_to_crisis_southkorea == `i'
}



************************************************************************************
* South Africa
************************************************************************************

gen     date_southafrica = 0
replace date_southafrica = 1 if inlist(year, 1977,1989)

gen relative_to_crisis_southafrica  = .

cap drop index_southafrica
gen      index_southafrica = 0
gen      index_southafrica_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace relative_to_crisis_southafrica = `i' if date_southafrica[_n+`j'] == 1
	replace index_southafrica              = SouthAfrica_HPI / SouthAfrica_HPI[_n+`j'] if relative_to_crisis_southafrica == `i'
	replace index_southafrica_c            = SouthAfrica_C / SouthAfrica_C[_n+`j'] if relative_to_crisis_southafrica == `i'7
}

	
**************************************************************
* Now Show Taylor + Schularick Figure
**************************************************************

preserve

foreach country in "australia" "belgium" "canada" "denmark" "finland" "france" "germany" "japan" "italy" "netherlands" "newzealand" "norway" "singapore" "spain" "southafrica" "southkorea" "sweden" "switzerland" "uk" "usa" {
	rename index_`country'_c index_c_`country'
}


keep year index* relative*

drop if year == .
reshape long index_ index_c_ relative_to_crisis_, i(year) j(country) string
	
gen n = 1 if index_ == 1
gen sd = index_ 

collapse (sum) n (mean) index_* (sd) sd , by(relative_to_crisis)

drop if relative_to_crisis == .
replace relative_to_crisis_ = relative_to_crisis_-3

twoway (line index_ relative_to_crisis_, lpattern(solid) lcolor(green) lwidth(thick)), ///
        ylabel(0.88(0.02)1.02) xlabel(-3(1)3) xtitle(Years Relative to Crisis) xline(0, lpattern(dash) lcolor(grey)) ytitle(Real House Prices Relative to Crisis) scheme(s1mono)

graph export "TS_Graph.pdf", replace 			
				
restore



********************************************************************************************************
********************************************************************************************************

* BARRO DATES

********************************************************************************************************
********************************************************************************************************

************************************************************************************
* Australia
************************************************************************************

cap drop date_australia_barro 
gen     date_australia_barro = 0
replace date_australia_barro = 1 if inlist(year, 1918, 1932, 1944)

cap drop rel_to_crisis_australia_barro  
gen rel_to_crisis_australia_barro  = .

cap drop index_australia_barro
gen      index_australia_barro = 0
gen      index_australia_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_australia_barro = `i' if date_australia_barro[_n+`j'] == 1
	replace index_australia_barro         = Australia_HPI / Australia_HPI[_n+`j'] if rel_to_crisis_australia_barro == `i'
	replace index_australia_barro_c       = Australia_C / Australia_C[_n+`j'] if rel_to_crisis_australia_barro == `i'
}

************************************************************************************
* France
************************************************************************************

cap drop date_france_barro 
gen      date_france_barro = 0
replace  date_france_barro = 1 if inlist(year,1871, 1915, 1943)

cap drop rel_to_crisis_france_barro  
gen rel_to_crisis_france_barro  = .

cap drop index_france_barro
gen      index_france_barro = 0
gen      index_france_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_france_barro = `i' if date_france_barro[_n+`j'] == 1
	replace index_france_barro         = France_HPI / France_HPI[_n+`j'] if rel_to_crisis_france_barro == `i'
	replace index_france_barro_c       = France_C / France_C[_n+`j'] if rel_to_crisis_france_barro == `i'
}

************************************************************************************
* Netherlands
************************************************************************************

cap drop date_netherlands_barro 
gen     date_netherlands_barro = 0
replace date_netherlands_barro = 1 if inlist(year,1893, 1918, 1944)

cap drop rel_to_crisis_netherlands_barro  
gen rel_to_crisis_netherlands_barro  = .

cap drop index_netherlands_barro
gen      index_netherlands_barro = 0
gen      index_netherlands_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_netherlands_barro = `i' if date_netherlands_barro[_n+`j'] == 1
	replace index_netherlands_barro         = Netherlands_HPI / Netherlands_HPI[_n+`j'] if rel_to_crisis_netherlands_barro == `i'
	replace index_netherlands_barro_c       = Netherlands_C / Netherlands_C[_n+`j'] if rel_to_crisis_netherlands_barro == `i'
}

************************************************************************************
* Norway
************************************************************************************

cap drop date_norway_barro 
gen     date_norway_barro = 0
replace date_norway_barro = 1 if inlist(year,1921, 1944)

cap drop rel_to_crisis_norway_barro  
gen rel_to_crisis_norway_barro  = .

cap drop index_norway_barro
gen      index_norway_barro = 0
gen      index_norway_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_norway_barro = `i' if date_norway_barro[_n+`j'] == 1
	replace index_norway_barro         = Norway_HPI / Norway_HPI[_n+`j'] if rel_to_crisis_norway_barro == `i'
	replace index_norway_barro_c       = Norway_C / Norway_C[_n+`j'] if rel_to_crisis_norway_barro == `i'
}

************************************************************************************
* USA	
************************************************************************************

cap drop date_usa_barro 
gen     date_usa_barro = 0
replace date_usa_barro = 1 if inlist(year, 1921, 1933)

cap drop rel_to_crisis_usa_barro  
gen rel_to_crisis_usa_barro  = .

cap drop index_usa_barro
gen      index_usa_barro = 0
gen      index_usa_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_usa_barro = `i' if date_usa_barro[_n+`j'] == 1
	replace index_usa_barro         = USA_HPI / USA_HPI[_n+`j'] if rel_to_crisis_usa_barro == `i'
	replace index_usa_barro_c       = USA_C / USA_C[_n+`j'] if rel_to_crisis_usa_barro == `i'
}



************************************************************************************
* Finland	
************************************************************************************

cap drop date_finland_barro 
gen     date_finland_barro = 0
replace date_finland_barro = 1 if inlist(year,1993)

cap drop rel_to_crisis_finland_barro  
gen rel_to_crisis_finland_barro  = .

cap drop index_finland_barro
gen      index_finland_barro = 0
gen      index_finland_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_finland_barro = `i' if date_finland_barro[_n+`j'] == 1
	replace index_finland_barro         = Finland_HPI / Finland_HPI[_n+`j'] if rel_to_crisis_finland_barro == `i'
	replace index_finland_barro_c       = Finland_C / Finland_C[_n+`j'] if rel_to_crisis_finland_barro == `i'
}


************************************************************************************
* South Korea
************************************************************************************

cap drop date_southkorea_barro 
gen     date_southkorea_barro = 0
replace date_southkorea_barro = 1 if inlist(year,1998)

cap drop rel_to_crisis_southkorea_barro  
gen rel_to_crisis_southkorea_barro  = .

cap drop index_southkorea_barro
gen      index_southkorea_barro = 0
gen      index_southkorea_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_southkorea_barro = `i' if date_southkorea_barro[_n+`j'] == 1
	replace index_southkorea_barro         = SouthKorea_HPI / SouthKorea_HPI[_n+`j'] if rel_to_crisis_southkorea_barro == `i'
	replace index_southkorea_barro_c       = SouthKorea_C / SouthKorea_C[_n+`j'] if rel_to_crisis_southkorea_barro == `i'
}


************************************************************************************
* Switzerland
************************************************************************************

cap drop date_switzerland_barro 
gen     date_switzerland_barro = 0
replace date_switzerland_barro = 1 if inlist(year,1998)

cap drop rel_to_crisis_switzerland_barro  
gen rel_to_crisis_switzerland_barro  = .

cap drop index_switzerland_barro
gen      index_switzerland_barro = 0
gen      index_switzerland_barro_c = 0

forval i = 0(1)6 {
local j = 3 - `i'
	replace rel_to_crisis_switzerland_barro = `i' if date_switzerland_barro[_n+`j'] == 1
	replace index_switzerland_barro         = Switzerland_HPI / Switzerland_HPI[_n+`j'] if rel_to_crisis_switzerland_barro == `i'
	replace index_switzerland_barro_c       = Switzerland_C / Switzerland_C[_n+`j'] if rel_to_crisis_switzerland_barro == `i'
}


*****************************************************************************************************
* Now Look at Average Stuff around Barro Dates
*****************************************************************************************************

foreach country in "australia" "finland" "france" "netherlands" "norway" "southkorea" "usa" "switzerland" {
	rename index_`country'_barro_c index_c_`country'_barro
}


preserve

keep year *_barro

drop if year == .
reshape long index_ index_c_ rel_to_crisis_, i(year) j(country) string

gen n = 1 if index_ == 1
gen sd = index_ 

collapse (sum) n (mean) index_ index_c_ (sd) sd , by(rel_to_crisis)

drop if rel_to_crisis == .
replace rel_to_crisis_ = rel_to_crisis_-3

twoway (line index_ rel_to_crisis_, lpattern(solid) lcolor(green) lwidth(thick)) (line index_c_ rel_to_crisis_, lpattern(dash) lcolor(blue) lwidth(thick)), ///
        ylabel(0.95(0.05)1.3) xlabel(-3(1)3)  xtitle(Years Relative to Consumption Trough) ytitle(Real House Prices and Consumption) xline(0, lpattern(dash) lcolor(grey)) scheme(s1mono) legend(label(1 "Real House Prices") label(2 "Real Consumption"))
		
graph export "Barro_Graph.pdf", replace 			
	
restore



*****************************************************************************************************
* DEVELOPMENT DURING WARS 
*****************************************************************************************************

preserve

keep if year > 1912 & year < 1919
keep year *_HPI

foreach country in "Australia" "Belgium" "Canada" "Denmark" "Finland" "France" "Germany" "Japan" "Italy" "Netherlands" "NewZealand" "Norway" "Singapore" "Spain" "SouthAfrica" "SouthKorea" "Sweden" "Switzerland" "UK" "USA" {
	rename `country'_HPI HPI_`country'
}

reshape long HPI_, i(year) j(country) string
drop if HPI_ == .
bysort country (year): gen hp_ww1 = HPI / HPI[1]
 
collapse (mean) hp, by(year)

gen number = _n
replace number = number - 1 
tempfile ww1
save `ww1'

restore

* WWII

preserve

keep if year > 1938 & year < 1946
keep year *_HPI

foreach country in "Australia" "Belgium" "Canada" "Denmark" "Finland" "France" "Germany" "Japan" "Italy" "Netherlands" "NewZealand" "Norway" "Singapore" "Spain" "SouthAfrica" "SouthKorea" "Sweden" "Switzerland" "UK" "USA" {
	rename `country'_HPI HPI_`country'
}

reshape long HPI_, i(year) j(country) string
drop if HPI_ == .
bysort country (year): gen hp_ww2 = HPI / HPI[1]

collapse (mean) hp, by(year)
rename year year_ww2

gen number = _n
replace number = number - 1 
mmerge number using `ww1'

twoway (scatter hp_ww1 number, mlabel(year) connect(line) mlabpos(2)) (scatter hp_ww2 number, mlabel(year_ww2) mlabpos(7) connect(line)) , ///
        scheme(s1mono) legend(label(1 "World War I") label(2 "World War II")) xtitle("Years Since Start of War") ytitle("Real House Prices")
		
graph export "Housing_During_Wars.pdf", replace 					

restore
